package cn.yitun.chat.yitunserverfilecore.service.impl;


import cn.yitun.chat.yituncommonutil.YitunMD5Util;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.common.FileDataInfo;
import cn.yitun.chat.yitunservercommonlib.util.FileNameUtil;
import cn.yitun.chat.yitunserverfilecore.component.AcceptFileHandler;
import cn.yitun.chat.yitunserverfilecore.component.data.FileInfo;
import cn.yitun.chat.yitunserverfilecore.dao.FileDataDao;
import cn.yitun.chat.yitunserverfilecore.manage.FileServerConfigManager;
import cn.yitun.chat.yitunserverfilecore.service.FileService;
import cn.yitun.chat.yitunserverfilecore.util.FilePathUtil;
import cn.yitun.chat.yitunserverfilecore.util.FileType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 *
 * @author yitun
 * @date 2015年12月30日 下午8:59:57
 * @version 0.0.1
 */
@Service
@Transactional
public class FileServiceImpl implements FileService{

    private String upload = "upload";
    @Autowired
    private FileDataDao fileDataDAO;
    @Resource
    private FileServerConfigManager fileServerConfigManager;

    AcceptFileHandler acceptFileHandler = new AcceptFileHandler();

    public List<FileDataInfo> uploadByIO(HttpServletRequest request, Long userId) {
        String basePath = request.getSession().getServletContext().getRealPath("/") + upload;
        String path = fileServerConfigManager.getSavePath();
        StringBuilder rootPath = new StringBuilder((YitunStringUtil.isEmpty(path)) ? basePath : path);// 拼接文件上传目录
        String nodePath = FilePathUtil.getNodePath("main/");
        List<FileInfo> list = acceptFileHandler.uploadByIO(request, rootPath.toString(), nodePath);
        return add(list, userId);
    }

    private List<FileDataInfo> add(List<FileInfo> fileInfoList, Long userId) {
        List<FileDataInfo> list = new ArrayList<>();

        if (null != fileInfoList && !fileInfoList.isEmpty()) {
            for (FileInfo fi : fileInfoList) {
                File file = fi.getFile();
                long length = file.length();
                String rootPath = fi.getRootPath();
                String nodePath = fi.getNodePath();
                String saveName = fi.getSaveName();
                String name = fi.getName();
                String suffixName = FileNameUtil.getSuffixName(name);

                FileDataInfo fd = new FileDataInfo();
                fd.setName(name);
                fd.setSaveName(saveName);
                fd.setSuffixName(suffixName);
                fd.setUserId(userId);
                fd.setSize(length);
                fd.setRootPath(rootPath);
                fd.setNodePath(nodePath);

                fd.setUrl("/files/" + nodePath + saveName);
                fd.setMd5(YitunMD5Util.md5L32(saveName));
                fd.setType(String.valueOf(FileType.dealFileType(suffixName)));
                fd.setCreateBy(userId);
                fd.setUpdateBy(userId);
                fileDataDAO.save(fd);

                list.add(fd);
            }
        }
        return list;
    }

    public FileDataInfo getFileData(Long id) {
        return this.fileDataDAO.get(id);
    }
}

